package com.naiterui.ehp.bs.emr.utils;

import java.net.URLDecoder;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.scale.ScaleUtil;

/**
 * 量表工具类
 *
 * @author yingsu
 * @date 2018/11/19 11:16
 * @since 1.0.0
 */
public class EmrScaleUtil {
    /**
     * 量表编号长度
     */
    //private static final int EXTBIZID_LENGTH = 23;
    /**
     * 量表编号名称
     */
    private static final String EXTBIZID_NAME = "extBizId";

    private static final String AES_ADD = "%2B";

    /**
     * 返回量表编号
     *
     * @param extBizId
     *
     * @return
     *
     * @throws BusinessException
     */
    public static String handleExtBizId(String extBizId) throws BusinessException {
        //判断是否是业务编号
        boolean flag = verifyExtBizId(extBizId);
        if (flag) {
            return extBizId;
        } else {//解密出业务编号
            //Aes解密
            String extBizId1;
            if (extBizId.contains(AES_ADD)) {
                extBizId1 = URLDecoder.decode(extBizId);
            } else {
                extBizId1 = extBizId;
            }
            //String extBizId1 = URLDecoder.decode(extBizId);
            Map<String, String> map = ScaleUtil.decryptField(extBizId1);
            if (map == null || map.get(EXTBIZID_NAME) == null) {
                return null;
            }
            return map.get(EXTBIZID_NAME);
        }
    }

    /**
     * 判断是否是量表业务id(true是业务id)
     *
     * @param extBizId
     *
     * @return
     */
    public static boolean verifyExtBizId(String extBizId) {
        //纯数字，并且长度是23的(两位固定标识(01:金数据)+两位填写场景(01:医生填写量表，02：患者填写量表）+13位时间戳+6位随机数)
        return StringUtils.isNumeric(extBizId);
    }

}
